home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / UnicodeClassMapping.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  102 lines

  1. /*
  2.  * @(#)UnicodeClassMapping.java    1.7 98/01/12
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996-1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996-1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. import java.lang.Character;
  34. /**
  35.  * This class maps categories to state change inputs for the
  36.  * WordBreakTable.  An entire category is mapped to the same
  37.  * value unless the character in question appears in the exception list.
  38.  */
  39. final class UnicodeClassMapping
  40. {
  41.     private int mappedValue[];
  42.     private SpecialMapping exceptionChars[];
  43.     private boolean hasException[];
  44.     private int asciiValues[];
  45.  
  46.     /**
  47.      * Create a mapping given a mapping from categories and a list
  48.      * of exceptions.  Both the mapping list and exceptionChars list must
  49.      * be sorted in ascending order.
  50.      */
  51.     public UnicodeClassMapping(int mappedValue[],
  52.                                SpecialMapping exceptionChars[],
  53.                                boolean hasException[],
  54.                                int asciiValues[])
  55.     {
  56.         this.mappedValue = mappedValue;
  57.         this.exceptionChars = exceptionChars;
  58.         this.hasException = hasException;
  59.         this.asciiValues = asciiValues;
  60.     }
  61.  
  62.     /**
  63.      * Map a character to a stage change input for WordBreakTable
  64.      * @param ch The character to map.
  65.      * @return The mapped value.
  66.      */
  67.     public int mappedChar(char ch)
  68.     {
  69.         if (ch <= 255)
  70.             return asciiValues[ch];
  71.  
  72.         // get an appropriate category based on the character's Unicode class
  73.         // if there's no entry in the exception table for that Unicode class,
  74.         // we're done; otherwise we have to look in the exception table for
  75.         // the character's category (\uffff is treated here as a sentinel
  76.         // value meaning "end of the string"-- we always look in the exception
  77.         // table for its category)
  78.         int    charType = Character.getType(ch);
  79.         if ((exceptionChars.length == 0) //|| (ch > '\u003f' && ch < '\u00a0')
  80.                 || (!hasException[charType] && ch != '\uffff')) {
  81.             return mappedValue[charType];
  82.         }
  83.  
  84.         //do binary search of exceptionChars table
  85.         int min = 0;
  86.         int max = exceptionChars.length - 1;
  87.         while (max > min) {
  88.             int pos = (max + min) >> 1;
  89.             if (ch > exceptionChars[pos].endChar)
  90.                 min = pos + 1;
  91.             else
  92.                 max = pos;
  93.         }
  94.         SpecialMapping sm = exceptionChars[min];
  95.         if (sm.startChar <= ch && ch <= sm.endChar)
  96.             return sm.newValue;
  97.         else
  98.             return mappedValue[charType];
  99.     }
  100. }
  101.  
  102.